<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="card-box">
      <div class="func-box">
        <el-input class="search-input" 
          v-model="query" 
          clearable
          @keyup.enter.native="getGoodsList"
        >
          <el-button slot='append' icon="el-icon-search"
            @click="getGoodsList"
          ></el-button>
        </el-input>
        <el-button type="primary"
          @click="handleAddGoodsOpen"
        >添加商品</el-button>
      </div>
      <!-- 商品列表 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" min-width="200"></el-table-column>
        <el-table-column prop="goods_price" label="商品单价(元)" width="110"></el-table-column>
        <el-table-column prop="goods_number" label="包含数量" width="110"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量(克)" width="110"></el-table-column>
        <el-table-column label="创建时间" width="160">
          <template v-slot="scope">
            {{scope.row.add_time|formatDateTime}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template v-slot="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"
              @click="handleGoodsEdit"
            ></el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete"
              @click="handleGoodsDelete(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页操作 -->
      <el-pagination
        layout="total,sizes,prev,pager,next,jumper"
        :total="total"
        :page-size="pagesize"
        :page-sizes="[5, 10, 15, 20]"
        :current-page="pagenum"
        @current-change="handlePagenumChange"
        @size-change="handlePagesizeChange"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
import {
  getGoodsByPage,
  deleteGoods
} from '../../../api/goods'
export default {
  created(){
    this.getGoodsList()
  },
  data(){
    return{
      query:'',
      pagenum:1,
      pagesize:5,
      total:0,
      "goodsList": []
    }
  },
  methods:{
    // 获取商品列表
    async getGoodsList(){
      try {
        const res = await getGoodsByPage({
          pagenum:this.pagenum,
          pagesize:this.pagesize,
          query:this.query
        })
        this.goodsList = res.data.goods
        this.total = res.data.total
      } catch (err) {
        console.log(err);
      }
    },
    handlePagenumChange(num){
      this.pagenum = num
      this.getGoodsList()
    },
    handlePagesizeChange(size){
      this.pagenum = 1
      this.pagesize = size
      this.getGoodsList()
    },
    // 删除商品
    handleGoodsDelete(id){
      this.$confirm('此操作将永久删除该商品信息, 是否继续?','删除商品',{
        type:'warning'
      }).then(async _=>{
        const res = await deleteGoods(id)
        this.$message({
          type:'success',
          message:res.meta.msg
        })
        this.getGoodsList()
      }).catch(_=>{
        this.$message('取消删除商品')
      })
    },
    // 修改商品
    handleGoodsEdit(){
      this.$message('暂时未实现修改商品')
    },
    // 打开添加商品页
    handleAddGoodsOpen(){
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style scoped>
.func-box{
  margin-bottom: 20px;
}
.search-input{
  min-width: 200px;
  max-width: 400px;
  margin-right: 20px;
}
</style>